/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.sequence.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.basic.domain.BasicAc02;
import me.zhengjie.modules.basic.domain.dto.BasicAc02QueryCriteria;
import me.zhengjie.modules.basic.mapper.BasicAc02Mapper;
import me.zhengjie.modules.basic.service.BasicAc02Service;
import me.zhengjie.modules.sequence.domain.Sequence;
import me.zhengjie.modules.sequence.mapper.SequenceMapper;
import me.zhengjie.modules.sequence.service.SequenceService;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
* @description 服务实现
* @author ly
* @date 2025-04-22
**/
@Service
@RequiredArgsConstructor
public class SequenceServiceImpl extends ServiceImpl<SequenceMapper, Sequence> implements SequenceService {

    private final SequenceMapper sequenceMapper;

    @Override
    public Sequence getSequence(Sequence criteria) {
        return sequenceMapper.getSequence(criteria);
    }

    @Override
    public int updateSequence(Sequence criteria) {
        return sequenceMapper.updateSequence(criteria);
    }

    @Override
    public String nextSequence(Sequence criteria) {
        String str = "";
        // 获取原序列
        Sequence sequence = sequenceMapper.getSequence(criteria);
        if ("aac001".equals(sequence.getName()) || "aab001".equals(sequence.getName())) {
            str = "530000" + sequence.getValue();
        }else if ("aaz159".equals(sequence.getName()) || "aaz040".equals(sequence.getName())) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
            str = "53" + simpleDateFormat.format(new Date())+ sequence.getValue();
        } else {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
            str = "53" + simpleDateFormat.format(new Date())+ sequence.getValue();
        }
        // 取完后自增
        sequenceMapper.updateSequence(criteria);

        return str;
    }
}
